#include "ToolbarFrame.h"
#include "FanfouContext.h"
#include "internal/ResourceManager.h"
#include "twcore/TwitWar.h"
#include <QResizeEvent>
namespace Fanfou
{
	ToolbarFrame::ToolbarFrame(QWidget* parent, ResourceManager& manager):
		QFrame(parent),
		resourceManager(manager)
	{
		setObjectName("ToolBarFrame");
		setGeometry(0, 365, 170, 32);

		m_mySelfBtn.setParent(this);
		m_mySelfBtn.setObjectName("mySelfBtn");
		m_mySelfBtn.setGeometry(7, 4, 24, 24);
		m_mySelfBtn.setCursor(Qt::PointingHandCursor);
		m_mySelfBtn.setFocusPolicy(Qt::NoFocus);
		m_mySelfBtn.installEventFilter(this);
		m_mySelfBtn.setScaledContents(true);
		m_mySelfBtn.setPixmap(QPixmap(FanfouContext::PluginIcon));
//		m_mySelfCountLabel.setParent(this);
//		m_mySelfCountLabel.setObjectName("countLabel");
//		m_mySelfCountLabel.setGeometry(18 ,4 ,15, 14);
//		m_mySelfCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
//		m_mySelfCountLabel.setAlignment(Qt::AlignCenter);
//		m_mySelfCountLabel.hide();

		m_homeBtn.setParent(this);
		m_homeBtn.setObjectName("timelineBtn");
		m_homeBtn.setGeometry(36, 4, 18, 24);
		m_homeBtn.setCursor(Qt::PointingHandCursor);
		m_homeBtn.setFocusPolicy(Qt::NoFocus);
		m_homeBtn.setCheckable(true);
		m_homeBtn.setChecked(true);
		m_toolButtonGroup.addButton(&m_homeBtn,BtnTimeline);
		m_homeLoadingLabel.setParent(this);
		m_homeLoadingLabel.setObjectName("loadingLabel");
		m_homeLoadingLabel.setGeometry(36,21,18,7);
		m_homeLoadingLabel.hide();
		m_homeLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_homeCountLabel.setParent(this);
		m_homeCountLabel.setObjectName("countLabel");
		m_homeCountLabel.setGeometry(44, 4, 15, 14);
		m_homeCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_homeCountLabel.setAlignment(Qt::AlignCenter);
		m_homeCountLabel.hide();

		m_atmeBtn.setParent(this);
		m_atmeBtn.setObjectName("atmeBtn");
		m_atmeBtn.setGeometry(60, 4, 18, 24);
		m_atmeBtn.setCursor(Qt::PointingHandCursor);
		m_atmeBtn.setFocusPolicy(Qt::NoFocus);
		m_atmeBtn.setCheckable(true);
		m_toolButtonGroup.addButton(&m_atmeBtn,BtnMention);
		m_atmeLoadingLabel.setParent(this);
		m_atmeLoadingLabel.setObjectName("loadingLabel");
		m_atmeLoadingLabel.setGeometry(60,21,18,7);
		m_atmeLoadingLabel.hide();
		m_atmeLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_atmeCountLabel.setParent(this);
		m_atmeCountLabel.setObjectName("countLabel");
		m_atmeCountLabel.setGeometry(68, 4, 15, 14);
		m_atmeCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_atmeCountLabel.setAlignment(Qt::AlignCenter);
		m_atmeCountLabel.hide();

		m_directBtn.setParent(this);
		m_directBtn.setObjectName("directBtn");
		m_directBtn.setGeometry(84, 4, 18, 24);
		m_directBtn.setCursor(Qt::PointingHandCursor);
		m_directBtn.setFocusPolicy(Qt::NoFocus);
		m_directBtn.setCheckable(true);
		m_toolButtonGroup.addButton(&m_directBtn,BtnDirectMessage);
		m_directLoadingLabel.setParent(this);
		m_directLoadingLabel.setObjectName("loadingLabel");
		m_directLoadingLabel.setGeometry(84,21,18,7);
		m_directLoadingLabel.hide();
		m_directLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_directCountLabel.setParent(this);
		m_directCountLabel.setObjectName("countLabel");
		m_directCountLabel.setGeometry(92, 4, 15, 14);
		m_directCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_directCountLabel.setAlignment(Qt::AlignCenter);
		m_directCountLabel.hide();

//		m_favBtn.setParent(this);
//		m_favBtn.setObjectName("favBtn");
//		m_favBtn.setGeometry(108, 4, 18, 24);
//		m_favBtn.setCursor(Qt::PointingHandCursor);
//		m_favBtn.setFocusPolicy(Qt::NoFocus);
//		m_favBtn.setCheckable(true);
//		m_toolButtonGroup.addButton(&m_favBtn,BtnFavorite);
//		m_favLoadingLabel.setParent(this);
//		m_favLoadingLabel.setObjectName("loadingLabel");
//		m_favLoadingLabel.setGeometry(108,21,18,7);
//		m_favLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
//		m_favLoadingLabel.hide();

		m_tweetBtn.setParent(this);
		m_tweetBtn.setObjectName("tweetBtn");
		m_tweetBtn.setGeometry(144, 4, 18, 24);
		m_tweetBtn.setCursor(Qt::PointingHandCursor);
		m_tweetBtn.setFocusPolicy(Qt::NoFocus);

		connect(&m_toolButtonGroup,SIGNAL(buttonClicked(int)),this,SIGNAL(buttonClicked(int)));
	}

	void ToolbarFrame::resizeEvent(QResizeEvent* e)
	{
		QFrame::resizeEvent(e);
		m_tweetBtn.move(e->size().width() - 8 - m_tweetBtn.width(), m_tweetBtn.y());
	}

	bool ToolbarFrame::eventFilter(QObject *, QEvent* e)
	{
		if(e->type() == QEvent::MouseButtonRelease)
		{
			emit myselfBtnClicked();
			return true;
		}
		return false;
	}

	void ToolbarFrame::showLoading(ToolbarButton button)
	{
		QLabel* target;
		switch(button)
		{
			case BtnTimeline:
				target = &m_homeLoadingLabel;
				break;
			case BtnMention:
				target = &m_atmeLoadingLabel;
				break;
			case BtnDirectMessage:
				target = &m_directLoadingLabel;
				break;
//			case BtnFavorite:
//				target = &m_favLoadingLabel;
//				break;
			default:
				return;
		}
		target->setMovie(resourceManager.getMovie(ToolbarMovie));
		target->show();
	}

	void ToolbarFrame::hideLoading(ToolbarButton button)
	{
		QLabel* target;
		switch(button)
		{
			case BtnTimeline:
				target = &m_homeLoadingLabel;
				break;
			case BtnMention:
				target = &m_atmeLoadingLabel;
				break;
			case BtnDirectMessage:
				target = &m_directLoadingLabel;
				break;
//			case BtnFavorite:
//				target = &m_favLoadingLabel;
//				break;
			default:
				return;
		}
		target->clear();
		target->hide();
		resourceManager.returnMovieResource(ToolbarMovie);
	}

	void ToolbarFrame::reduceWarningCount(ToolbarButton button)
	{
		QLabel* target;
		switch(button)
		{
//			case BtnMyself:
//				target = &m_mySelfCountLabel;
//				break;
			case BtnTimeline:
				target = &m_homeCountLabel;
				break;
			case BtnMention:
				target = &m_atmeCountLabel;
				break;
			case BtnDirectMessage:
				target = &m_directCountLabel;
				break;
			default:
				return;
		}

		int count = target->text().toInt();
		if(count > 1)
		{
			target->show();
			target->setText(QString::number(count-1));
		}else
		{
			target->clear();
			target->hide();
		}
	}

	void ToolbarFrame::setWarningCount(ToolbarButton button,int count)
	{
		if(count < 0)
			count = 0;
		QLabel* target;
		switch(button)
		{
//			case BtnMyself:
//				target = &m_mySelfCountLabel;
//				break;
			case BtnTimeline:
				target = &m_homeCountLabel;
				break;
			case BtnMention:
				target = &m_atmeCountLabel;
				break;
			case BtnDirectMessage:
				target = &m_directCountLabel;
				break;
			default:
				return;
		}

		if(count > 0)
		{
			target->show();
			target->setText(QString::number(count));
		}else
		{
			target->clear();
			target->hide();
		}
	}

	bool ToolbarFrame::hasNewWarning()
	{
		return !(m_homeCountLabel.isHidden() && m_atmeCountLabel.isHidden() && m_directCountLabel.isHidden());
	}
} // namespace Sina
